home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch18
/
fig18_09.txt
< prev
Wrap
Text File
|
1998-02-27
|
786b
|
36 lines
1 // Fig. 18.9: fig18_09.cpp
2 // Using an anonymous union
3 #include <iostream.h>
4
5 int main()
6 {
7 // Declare an anonymous union.
8 // Note that members b, d, and f share the same space.
9 union {
10 int b;
11 double d;
12 char *f;
13 };
14
15 // Declare conventional local variables
16 int a = 1;
17 double c = 3.3;
18 char *e = "Anonymous";
19
20 // Assign a value to each union member
21 // successively and print each.
22 cout << a << ' ';
23 b = 2;
24 cout << b << endl;
25
26 cout << c << ' ';
27 d = 4.4;
28 cout << d << endl;
29
30 cout << e << ' ';
31 f = "union";
32 cout << f << endl;
33
34 return 0;
35 }